/***************************************************************************
 *   Copyright (C) 2011 by The rPem3d developers                           *
 *   elseifkk@users.sf.net                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "cvcls.h"
#include "palette.h"

#include <qpixmap.h>
#include <qpainter.h>
#include <qbrush.h>

CrystalViewer::~CrystalViewer() {
}
void CrystalViewer::closeEvent(QCloseEvent *e) {
    e->accept();
    emit done(pmtid);
}
CrystalViewer::CrystalViewer ( QWidget *parent, const char *name )
: QWidget ( parent, name ) {
    nx=0;
    ny=0;
    hist=NULL;
    pm=NULL;
    timerid=0;
    pmtid=-1;
}

void  CrystalViewer::initCrystalViewer(int numx, int numy, int *h, int id) {
    nx=numx;
    ny=numy;
    pmtid=id;
    if (nx<=128&&ny<=128) {
        np=npix_def;
        msec=msec_def;
    } else {
        np=1;
        msec=10000;
    }
    hist=h;
    setFixedSize(nx*np,ny*np);
    if (pm==NULL) pm=new QPixmap(size());
    pm->fill("black");
    drawHitPattern();
}
void CrystalViewer::startView() {
    timerid=startTimer(msec);
}
int CrystalViewer::getMaxVal() {
    int m=0;
    for (int iy=0;iy<ny;iy++) {
        for (int ix=0;ix<nx;ix++) {
            if (m<hist[ix+iy*nx]) m=hist[ix+iy*nx];
        }
    }
    return m;
}
void CrystalViewer::drawHitPattern() {
    int cmax=getMaxVal();
    if (cmax==0) {
        pm->fill("black");
        return;
    }
    int c;
    QBrush b(QBrush::SolidPattern);
    QPainter p(pm);
    for (int iy=0;iy<ny;iy++) {
        for (int ix=0;ix<nx;ix++) {
            c=(float)hist[ix+iy*nx]/(float)cmax*255;
            if (c==0) {
                b.setColor("black");
            } else {
                b.setColor(qRgb(
                               mypalette[c].r,
                               mypalette[c].g,
                               mypalette[c].b
                           ));
            }
            p.fillRect(ix*np,iy*np,np,np,b);
        }
    }

}
void CrystalViewer::timerEvent(QTimerEvent *e) {
    drawHitPattern();
    update();
}
void CrystalViewer::paintEvent(QPaintEvent *e) {
    bitBlt(this,0,0,pm);
}
void CrystalViewer::keyPressEvent(QKeyEvent *e) {
    switch (e->key()) {
    case Qt::Key_Q:
    case Qt::Key_Escape:
        close();
        break;
    }
}
